package tum0r.webengine.models.device

import java.io.File
import java.util.*

/**
 * 说明: 设备信息
 *
 * 创建者: tum0r
 *
 * 时间: 2021/8/28 14:00
 */
class DeviceInfo private constructor() {
    /**
     * 操作系统信息
     */
    var os: OS = OS(System.getProperty("sun.cpu.endian"), System.getProperty("os.arch"), System.getProperty("sun.arch.data.model"), System.getProperty("user.name"), System.getProperty("user.dir"), System.getProperty("os.name"), System.getProperty("os.version"))

    /**
     * 系统所有shell或cmd文件路径
     */
    val shells = LinkedHashMap<String, String>()

    /**
     * CPU核心数（物理cpu数x单个cpu核心数）
     */
    val cpuNumber: Int = Runtime.getRuntime().availableProcessors()

    companion object {
        private var deviceInfo: DeviceInfo? = null

        @JvmStatic
        fun getInstance(): DeviceInfo {
            if (deviceInfo == null) deviceInfo = DeviceInfo()
            return deviceInfo!!
        }
    }

    init {
        val osName = os.name.lowercase(Locale.getDefault())
        if (osName.contains("mac") || osName.contains("linux")) {
            val lines = File("/etc/shells").readLines()
            for (line in lines) if (line.trim() != "" && !line.startsWith("#")) shells[line.substring(if (line.lastIndexOf("/") != -1) line.lastIndexOf("/") + 1 else 0).lowercase(Locale.getDefault())] = line
        } else {
            shells["cmd"] = "C:\\Windows\\system32\\cmd.exe"
        }
    }
}